package com.t3go.t3_map.business.wait;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shareware.common.ApplicationKt;
import com.shareware.common.listener.Callback;
import com.shareware.common.utils.LogExtKt;
import com.shareware.common.utils.ScreenExtKt;
import com.t3go.passenger.base.utils.JsonUtils;
import com.t3go.t3_map.R;
import com.t3go.t3_map.business.common.MapHelper;
import com.t3go.t3_map.entity.Annotation;
import com.t3go.t3_map.entity.GuideEntity;
import com.t3go.t3_map.entity.Speed;
import com.t3go.t3_map.entity.WaitArgs;
import com.t3map.api.map.T3Map;
import com.t3map.api.map.model.T3LatLng;
import com.t3map.api.map.model.T3Marker;
import com.t3map.api.map.model.T3MarkerOptions;

public class WaitMapHelper extends MapHelper {

    private static final String TAG = "WaitMapHelper";

    private final WaitInfoWindowAdapter mAdapter;

    private T3Marker mWaitingMarker;
    private Callback<T3Marker> mCallback;

    public WaitMapHelper(Context context, T3Map map) {
        super(context, map);
        mAdapter = new WaitInfoWindowAdapter();
        mAdapter.setWalkGlideClickListener(scene -> {
            if (mCallback != null) {
                mCallback.call(mWaitingMarker);
            }
        });

        mMap.setInfoWindowAdapter(mAdapter);
    }

    public T3Marker addWaitingMarker(Annotation data) {
        if (data == null) {
            return null;
        }
        WaitArgs args = JsonUtils.fromJson(JsonUtils.toJson(data.getArgs()), WaitArgs.class);
        if (args == null) {
            return null;
        }

        T3LatLng position = new T3LatLng(data.getLatitude(), data.getLongitude());
        mWaitingMarker = addMarker(position);
        if (mWaitingMarker == null) {
            return null;
        }
        mWaitingMarker.showInfoWindow();

        updateAdapter(args);

        return mWaitingMarker;
    }

    private void updateAdapter(WaitArgs args) {
        if (mAdapter == null || args == null) {
            return;
        }

        mAdapter.showPositiveTime(args.getBubbleText(), args.getLimitStopTips());
        mAdapter.showPositiveTimeLoading(args.getAddress());

        Speed speed;
        if (args.getWaterAnimationMultiple() > 2) {
            speed = Speed.DOUBLE;
        } else if (args.getWaterAnimationMultiple() > 1) {
            speed = Speed.FAST;
        } else {
            speed = Speed.NORMAL;
        }
        mAdapter.updateWaitAnimationRate(speed);
        mAdapter.updatePositiveIcon(speed == Speed.FAST || speed == Speed.DOUBLE);

        GuideEntity guide = new GuideEntity();
        guide.setGuideMainUrl(args.getGuideMainUrl());
        guide.setGuidePageUrl(args.getGuidePageUrl());
        mAdapter.updateBubbleGuideData(guide);
    }

    public T3Marker addMarker(T3LatLng position) {
        LogExtKt.log(TAG, "addMarker -> position = " + JsonUtils.toJson(position));
        if (mMap == null) {
            return null;
        }
        if (position == null) {
            return null;
        }
        View iconView = buildIconMarkerView(R.drawable.icon_wait_transparent);
        T3MarkerOptions options = new T3MarkerOptions()
                .position(position)
                .iconView(iconView)
                .viewInfoWindow(true)
                .draggable(false)
                .anchor(0.5f, 0.5f);
        T3Marker marker = mMap.addMarker(options);
        if (marker == null) {
            return null;
        }

        int offset = iconView.getHeight();
        if (offset == 0) {
            iconView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            offset = iconView.getMeasuredHeight();
        }
        marker.setInfoWindowOffset(0, ScreenExtKt.dp2px(280) + offset / 2);
        marker.setClickable(false);
        marker.setObject(position);
        return marker;
    }

    public static View buildIconMarkerView(int resId) {
        ViewGroup view = (ViewGroup) LayoutInflater.from(ApplicationKt.getContextGlobal()).inflate(R.layout.map_marker_icon_view, null);
        ImageView imageView = view.findViewById(R.id.image_marker);
        imageView.setImageResource(resId);
        return view;
    }

    public void updateWaitingMarker(T3Marker marker, Annotation data) {
        if (marker == null || data == null) {
            return;
        }
        WaitArgs args = JsonUtils.fromJson(JsonUtils.toJson(data.getArgs()), WaitArgs.class);
        if (args == null) {
            return;
        }

        T3LatLng position = new T3LatLng(data.getLatitude(), data.getLongitude());
        marker.setPosition(position);

        updateAdapter(args);
    }

    public boolean isWaitingMarker(T3Marker marker) {
        return mWaitingMarker == marker;
    }

    public void removeWaitingMarker() {
        mWaitingMarker = null;
        mAdapter.cancelWaitAnim();
    }

    public void setRouteGuideListener(Callback<T3Marker> callback) {
        mCallback = callback;
    }

    public void onResume() {
        mAdapter.startWaitAnim();
    }

    public void onPause() {
        mAdapter.cancelWaitAnim();
    }
}
